package com.hqyj.demo.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.demo.entity.Admin;
import com.hqyj.demo.entity.vo.LikeQueryVo;
import com.hqyj.demo.utils.CacheConstantsKey;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.hqyj.demo.service.MenuService;
import com.hqyj.demo.entity.Menu;
import com.hqyj.demo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhouwenjun
 * @since 2022-10-10
 */

@RestController
@RequestMapping("/menu")
public class MenuController {
     @Autowired
     private MenuService menuService;

    // 修改或新增
    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody Menu menu){
        boolean result;
        System.out.println(menu);
        if (menu.getId () != 0) {
            result = menuService.updateById(menu);
        } else {
            result = menuService.save(menu);
        }
        if(result){
            return R.ok().message("保存数据成功！");
        }else{
            return R.error().message("保存数据失败！");
        }
    }

     //    保存方法
     @PostMapping("/save")
     public R save(@RequestBody Menu menu){
        boolean save = menuService.save(menu);
        if(save){
           return R.ok().message("保存数据成功！");
        }else{
           return R.error().message("保存数据失败！");
        }
     }
     //    删除单条数据
     @DeleteMapping("/delOne/{id}")
     public R delOne(@PathVariable Integer id){
        boolean b = menuService.removeById(id);
        if(b)
           return R.ok().message("数据删除成功");
        else
           return R.ok().message("数据删除失败");
     }
     //  批量删除
     @DeleteMapping("/delBatch")
     public R delBatch(@RequestBody List<Integer> ids){
        boolean b = menuService.removeBatchByIds(ids);
        if(b)
           return R.ok().message("批量删除成功");
        else
           return R.ok().message("批量删除失败");
     }
    //    修改数据
     @PutMapping("/update")
     public R update(@RequestBody Menu menu){
        boolean save = menuService.updateById(menu);
        if(save){
           return R.ok().message("数据修改成功！");
        }else{
          return R.error().message("数据修改失败！");
        }
     }

     //修改多条记录状态
     @PutMapping("/saveUpdate")
     public R saveUpdate(@RequestBody List<Menu> list) {
          //logger.info (list.toString ());
        if (list != null) {
             boolean result = menuService.updateBatchById (list);
             if (result) {
                  return R.ok ().message ("保存成功！");
             }
             return R.error ().message ("保存失败!");
        }
        return R.error ().message ("没有参数!");
     }

     //查询已登录用户拥有的菜单
    @PostMapping("/menuListByAdmin")
    public R menuList(){
        Admin admin = (Admin) SecurityUtils.getSubject().getSession().getAttribute(CacheConstantsKey.LOGIN_TOKEN_KEY);
        //System.out.println(admin);
        List<Menu> menus = menuService.menuList(admin.getId());
        //System.out.println(menus);
        return R.ok().message("获取角色菜单成功").data("menus",menus);
    }

    //查询所有菜单
    @PostMapping("/allMenuList")
    public R allMenuList(){
        List<Menu> menus = menuService.allMenuList();
        return R.ok().message("获取所有菜单信息成功").data("menuList",menus);
    }


//     //    模糊查询方法
     @PostMapping("/likeQuery")
     public R likeParamsQuery(@RequestBody LikeQueryVo likeQueryVo) {
         QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
         if (!StringUtils.isEmpty(likeQueryVo.getName())) {
             queryWrapper.like("name", likeQueryVo.getName());
         }

         //分页查询
        Page<Menu> page = menuService.page(new Page<>(likeQueryVo.getCurrentPage (), likeQueryVo.getSizePage ()), queryWrapper);
        long total = page.getTotal();//总记录数
        List<Menu> list = page.getRecords();
        return R.ok().message ("查询成功！").data ("list",list).data ("total",total);
     }
}
